﻿using IShoppingBll;
using IShoppingDal;
using ShoppingEntity;
using ShoppingEntity.GetMenuInfo;
using ShoppingEntity.ResultModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingBll
{
    /// <summary>
    /// 菜单业务处理层
    /// </summary>
    public class MenuInfoBll : BaseDeleteBll<MenuInfo>, IMenuInfoBll
    {
        private IMenuInfoDal _menuInfoDal;
        private IMenuInfo_RoleInfoDal _menuInfo_RoleInfoDal;
        private IUserInfo_RoleInfoDal _userInfo_RoleInfoDal;
        private IRoleInfoDal _roleInfo;
        public MenuInfoBll(IMenuInfoDal menuInfoDal
            , IMenuInfo_RoleInfoDal menuInfo_RoleInfoDal
            , IUserInfo_RoleInfoDal userInfo_RoleInfoDal
            , IRoleInfoDal roleInfo) : base(menuInfoDal)
        {
            _menuInfoDal = menuInfoDal;
            _menuInfo_RoleInfoDal = menuInfo_RoleInfoDal;
            _userInfo_RoleInfoDal = userInfo_RoleInfoDal;
            _roleInfo = roleInfo;
        }
        /// <summary>
        /// 获取菜单集合信息
        /// </summary>
        /// <returns></returns>
        public List<MenuInfoResultModel> GetInitMenus(UserInfo userInfo)
        {
            //判断用户信息里IsAdmin是否等于true，并且他的角色是“系统管理员”才能显示所有视图
            int conunt = (from ur in _userInfo_RoleInfoDal.GetEntitiesDb().Where(r => r.UserId == userInfo.ID)
                          join r in _roleInfo.GetEntitiesDb().Where(r => !r.IsDelete && r.RoleName == "系统管理员")
                          on ur.RoleID equals r.ID
                          select ur.ID).Count();
            //三目表达式
            bool isHad = conunt > 0 ? true : false;

            List<GetMenuInfo> menus = new List<GetMenuInfo>();
            //超级管理员
            if (userInfo.IsAdmin == true && isHad == true)
            {
                //获取所有菜单集合信息
                menus = _menuInfoDal.GetEntitiesDb().Where(m => !m.IsDelete).OrderBy(m => m.Sort).Select(m => new GetMenuInfo
                {
                    ID = m.ID,
                    Title = m.Title,
                    Icon = m.Icon,
                    Href = m.Href,
                    Target = m.Target,
                    Level = m.Level,
                    ParentId = m.ParentId
                }).ToList();
            }
            //普通用户
            else
            {
                //根据用户id获取用户的角色
                var roleInfoIds = _userInfo_RoleInfoDal.GetEntitiesDb().Where(u => u.UserId == userInfo.ID).Select(u => u.RoleID).ToList();

                //获取菜单的id集合
                var menuIds = _menuInfo_RoleInfoDal.GetEntitiesDb().Where(r => roleInfoIds.Contains(r.RoleId)).Select(r => r.MenuId).ToList();

                //多表连接根据角色获取菜单

                menus = (from m in _menuInfoDal.GetEntitiesDb().Where(m => menuIds.Contains(m.ID))

                         select new
                         {
                             ID = m.ID,
                             Title = m.Title,
                             Icon = m.Icon,
                             Href = m.Href,
                             Target = m.Target,
                             Level = m.Level,
                             ParentId = m.ParentId,
                             m.Sort
                         }).OrderBy(m => m.Sort).Select(m => new GetMenuInfo
                         {
                             ID = m.ID,
                             Title = m.Title,
                             Icon = m.Icon,
                             Href = m.Href,
                             Target = m.Target,
                             Level = m.Level,
                             ParentId = m.ParentId
                         }).ToList();

            }

            //获取父级菜单集合信息
            List<MenuInfoResultModel> menuInfos = menus.Where(m => m.Level == 0 && m.ParentId == null).Select(m => new MenuInfoResultModel
            {
                ID = m.ID,
                href = m.Href,
                icon = m.Icon,
                target = m.Target,
                title = m.Title,

            }).ToList();

            foreach (var menuInfo in menuInfos)
            {
                //查询父级菜单自己的菜单
                var childMenus = menus.Where(m => m.ParentId == menuInfo.ID).Select(m => new MenuInfoResultModel
                {

                    ID = m.ID,
                    href = m.Href,
                    icon = m.Icon,
                    target = m.Target,
                    title = m.Title,
                }).ToList();

                //为父级菜单添加子集菜单
                menuInfo.child = childMenus;

                //递归赋值子菜单
                RecursionMenu(menuInfo.child, menus);
            }
            return menuInfos;
        }
        /// <summary>
        /// 递归赋值菜单集合
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="menus1"></param>
        public void RecursionMenu(List<MenuInfoResultModel> menus, List<GetMenuInfo> menus1)
        {
            foreach (var item in menus)
            {
                var childmenus = menus1.Where(m => m.ParentId == item.ID).Select(m => new MenuInfoResultModel
                {
                    ID = m.ID,
                    href = m.Href,
                    icon = m.Icon,
                    target = m.Target,
                    title = m.Title,
                }).ToList();
                //为父级菜单添加子集菜单
                item.child = childmenus;
                RecursionMenu(item.child, menus1);
            }
        }
        /// <summary>
        /// 菜单数据集显示(分页)
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="title"></param>
        /// <param name="describe"></param>
        /// <returns></returns>
        public object GetMenuInfoByPage(int page, int limit, out int count, string title, string describe)
        {
            //获取数据库中全部没删除的数据(未真实查询)
            var menuInfos = _menuInfoDal.GetEntitiesDb().Where(u => u.IsDelete == false);
            if (!string.IsNullOrEmpty(title))
            {
                menuInfos = menuInfos.Where(m => m.Title.Contains(title));
            }
            if (!string.IsNullOrEmpty(describe))
            {
                menuInfos = menuInfos.Where(u => u.Describe.Contains(describe));
            }

            //查询出来的数据的数量
            count = menuInfos.Count();

            //连表查询
            var timpList = (from me in menuInfos
                            join md in _menuInfoDal.GetEntitiesDb()
                            on me.ParentId equals md.ID into mttmpe
                            from mt in mttmpe.DefaultIfEmpty()
                            select new
                            {
                                me.ID,
                                me.Title,
                                me.Describe,
                                me.Level,
                                me.Sort,
                                me.Href,
                                me.ParentId,
                                me.Icon,
                                me.Target,
                                me.IsDelete,
                                //me.DeleteTime,
                                me.CreateTime,
                                ParentTitle = mt.Title
                            }).OrderBy(m => m.Sort).Skip((page - 1) * limit).Take(limit).ToList().Select(m =>
                            {
                                return new
                                {
                                    m.ID,
                                    m.Title,
                                    m.Describe,
                                    m.Level,
                                    m.Sort,
                                    m.Href,
                                    m.ParentId,
                                    m.Icon,
                                    m.Target,
                                    m.IsDelete,
                                    CreateTime = m.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                                    m.ParentTitle
                                };
                            });
            return timpList;
        }
        /// <summary>
        /// 获取菜单下拉框选数据
        /// </summary>
        /// <returns></returns>
        public object GetMenuInfoSelectOption()
        {
            var menuInfos = _menuInfoDal.GetEntitiesDb().Where(m => !m.IsDelete).OrderBy(m => m.Sort).Select(m => new
            {

                m.ID,
                m.Title
            });
            return menuInfos;
        }
        /// <summary>
        /// 获取菜单下拉框选数据(不能选当前id为父级)
        /// </summary>
        /// <returns></returns>
        public object GetMenuInfoSelectOption(string menuInfoId)
        {
            var menuInfos = _menuInfoDal.GetEntitiesDb().Where(m => !m.IsDelete && m.ID != menuInfoId).Select(m => new
            {
                m.ID,
                m.Title
            });
            return menuInfos;
        }
        /// <summary>
        /// 菜单修改
        /// </summary>
        /// <returns></returns>
        public bool UpdateMenuInfo(string ID, string title, string describe, int level, int sort, string href, string parentId, string icon, string target)
        {
            MenuInfo menuInfo = _menuInfoDal.GetEntity(ID);
            if (menuInfo != null)
            {
                menuInfo.ID = ID;
                menuInfo.Title = title;
                menuInfo.Describe = describe;
                menuInfo.Sort = sort;
                menuInfo.Level = level;
                menuInfo.ParentId = parentId;
                menuInfo.Icon = icon;
                menuInfo.Target = target;
                return _menuInfoDal.Update(menuInfo);
            }
            else
            {
                return false;
            }
        }
    }
}